//
//  XUEmotionController.m
//  新浪微博
//
//  Created by xuwenxin on 15-4-18.
//  Copyright (c) 2015年 xuwenxin. All rights reserved.
//

#import "XUEmotionController.h"
#import "XUEmotion.h"
#import "XUEmotionViewCell.h"
#import "XUEmotionModel.h"
#import "XUEmoticonSectionModel.h"

@interface XUEmotionController () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) NSArray *emotions;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionEmotion;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layoutEmotion;

@end

@implementation XUEmotionController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    CGFloat margin = 10;
    CGFloat itmeW = ([UIScreen mainScreen].bounds.size.width - margin * 8) / 7;
    CGFloat itmeH = itmeW;
    self.layoutEmotion.itemSize = CGSizeMake(itmeW, itmeH);
    
    self.layoutEmotion.minimumInteritemSpacing = margin;
    self.layoutEmotion.minimumLineSpacing = margin;
    
    self.layoutEmotion.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin);
    // Do any additional setup after loading the view.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.emotions.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    XUEmoticonSectionModel *model = self.emotions[section];
    return model.sectionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XUEmotionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    XUEmoticonSectionModel *sectionModel = self.emotions[indexPath.section];
    cell.model = sectionModel.sectionArray[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"%@",indexPath);
//    XUEmoticonSectionModel *sectionModel = self.emotions[indexPath.section];
//    XUEmotionModel *dict = sectionModel.sectionArray[indexPath.item];
    if (self.myblock) {
        self.myblock(self.emotions,indexPath);
  
    }
}



- (IBAction)emotionType:(id)sender {
    
    UIButton *btn = sender;
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    // 遍历这个数组,找到跟自己标题一样的组,,直接取下标
    for (int i = 0; i<self.emotions.count; i++) {
        XUEmoticonSectionModel *model = self.emotions[i];
        if ([btn.titleLabel.text isEqualToString:model.sectionTitle]) {
            [self.collectionEmotion setContentOffset:CGPointMake(screenW * i, 0) animated:YES];
            return;
        }
    }
    
}

- (NSArray *)emotions
{
    if (_emotions == nil) {
        _emotions = [XUEmotion Emotion];
    }
    return _emotions;
}

@end
